package com.cs.framework.common;


import com.cs.framework.domain.R;

/**
 * <p>
 *      业务编码
 * </p>
 * @author wind
 * @date    2024/12/28 17:51
 * @version v1.0
 */
public enum HttpCode {

    SUCCESS(1, "success"),
    FAIL(0, "fail"),

    PARAM_ERROR(400, "param error"),
    SERVER_ERROR(500, "server error"),
    ;

    private final Integer code;

    private final String msg;

    HttpCode(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static R success(String msg, Object data) {
        return new R(SUCCESS.code, msg, data);
    }

    public static R success(Object data) {
        return new R(SUCCESS.code, SUCCESS.msg, data);
    }

    public static R success() {
        return new R(SUCCESS.code, SUCCESS.msg, null);
    }

    public static R fail(String msg) {
        return new R(FAIL.code, msg, null);
    }

    public static R fail() {
        return new R(FAIL.code, FAIL.msg, null);
    }

    public static R flag(boolean flag) {
        return flag ? success() : fail();
    }

    public static R flag(boolean flag, Object data) {
        return flag ? success(data) : fail();
    }

    public static R serverError() {
        return new R(SERVER_ERROR.code, SERVER_ERROR.msg, null);
    }

    public static R paramError(String msg) {
        return new R(PARAM_ERROR.code, msg, null);
    }

    /**
     * 返回结果
     * @return
     */
    public R result() {
        return new R(code, msg, null);
    }
}
